/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.dialogs;

import java.util.Date;  
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.mags.common.EngineContext;
import org.mags.object.common.ParserFormatterUtils;
import org.mags.remas.model.entity.CommentType;
import org.mags.remas.model.entity.EntityType;
import org.mags.remas.model.entity.KeyDefinition;
import org.mags.remas.model.entity.provider.EnumValueProvider;
import org.mags.remas.model.entity.provider.KeyTypeProvider;
import org.mags.remas.view.commons.IChangeFocusListener;
import org.mags.remas.view.widgets.FieldsControl;

/**
 * 
 * @author Mario Gaitan
 *
 */
public abstract class AbstractFindRelationshipsDialog extends AbstractDialog 
	implements IChangeFocusListener {
    private Button cancelButton;
    private Button acceptButton;
    private FieldsControl fieldsControl;
    private Text txtName;
    private Text txtCode;
    private Text txtDescription;
    private Text txtComment;
    private Combo cmbEntityType;
    private List<EntityType> entityTypeList; 
    private List<CommentType> commentTypeList;
    private Composite keys;
    public FieldsControl getFieldsControl() {
    	return fieldsControl;
    }
    public Map<KeyDefinition, Object> getKeys() {
    	Map<KeyDefinition, Object> values = new HashMap<KeyDefinition, Object>();
    	if(getEntityType()!=null) {
	    	List<KeyDefinition> keys = EngineContext.getDataContext().getEntityProviderFactory().getKeyDefinitionProvider().getByEntityTypeId(getEntityType().getEntityTypeId());
			for(KeyDefinition i: keys) {
				Object value = fieldsControl.getFromControl("propertyidx_"+i.getName());
				if(value!=null) 
					values.put(i, value);
			}    	
    	}
    	return values;
    }
    public EntityType getEntityType() {
    	return (EntityType)fieldsControl.getFromCombo("property_type");
    }
    public String getComment() {
    	return txtComment.getText();
    }
    public CommentType getCommentType() {
    	return (CommentType)fieldsControl.getFromCombo("property_comment_type");
    }
    public String getDescription() {
    	return txtDescription.getText();
    }
    public String getCode() {
    	return txtCode.getText();
    }
    public String getEntityName() {
    	return txtName.getText();
    }
	@Override
	public void changeFocus(FocusEvent e) {
	}
    public AbstractFindRelationshipsDialog(Display display) {
    	super(display, SWT.RESIZE);
    }
    private void changeEntityType(EntityType entityType) {
    	List<KeyDefinition> keys = EngineContext.getDataContext().getEntityProviderFactory().getKeyDefinitionProvider().getAll();
    	setVisibleKeys(keys, false);
    	keys = EngineContext.getDataContext().getEntityProviderFactory().getKeyDefinitionProvider().getByEntityTypeId(entityType.getEntityTypeId());
    	setVisibleKeys(keys, true);
    }
    private void setVisibleKeys(List<KeyDefinition> keys, boolean visible) {
    	if(keys!=null&&keys.size()>0) {
			KeyTypeProvider keyTypeProvider = EngineContext.getDataContext().getEntityProviderFactory().getKeyTypeProvider();
			EnumValueProvider enumValueProvider = EngineContext.getDataContext().getEntityProviderFactory().getEnumValueProvider();
			String label = null;
			for(KeyDefinition i: keys) {
				if(visible) {
					label = i.getName();
					if(keyTypeProvider.isENUM(i.getKeyType())) {
						fieldsControl.addCombo("keys", "propertyidx_"+i.getName(), label, enumValueProvider.getByEnumNameId(i.getEnumName().getEnumNameId()), "evalue", null).setData(i);
					}
					else if(keyTypeProvider.isLIST(i.getKeyType())) {
						fieldsControl.addText("keys", "propertyidx_"+i.getName(), label, 255, "").setData(i);
					}
					else {
						if(keyTypeProvider.isDATE(i.getKeyType())) 
							fieldsControl.addTextDate("keys", "propertyidx_"+i.getName(), label+" [" + ParserFormatterUtils.getInstance().getDatePattern().toLowerCase() + "]", new Date()).setData(i);
						else if(keyTypeProvider.isINTEGER(i.getKeyType()))
							fieldsControl.addText("keys", "propertyidx_"+i.getName(), label, 15, "0").setData(i);
						else if(keyTypeProvider.isFLOAT(i.getKeyType()))
							fieldsControl.addText("keys", "propertyidx_"+i.getName(), label, 15, "0").setData(i);				
						else 
							fieldsControl.addText("keys", "propertyidx_"+i.getName(), label, 255, "").setData(i);
					}
				}  else {
					fieldsControl.dispose("propertyidx_"+i.getName());
				}
			}
    	}		    	
    	this.keys.layout(true);
    	getShell().pack();
    }
    protected void createPartControl(Composite parent)  {
    	parent.setLayout(new GridLayout(1, true));
    	fieldsControl = new FieldsControl(this);
		fieldsControl.setParent(parent);
		fieldsControl.setDisplay(getShell().getDisplay());
		fieldsControl.addContainer("properties", "PROPERTIES");
		txtName = fieldsControl.addText("properties", "property_name", "name", 255, "");
		txtCode = fieldsControl.addText("properties", "property_code", "code", 255, "");
		txtDescription = fieldsControl.addText("properties", "property_description", "description", 255, "");
		commentTypeList = EngineContext.getDataContext().getEntityProviderFactory().getCommentTypeProvider().getAll();
		fieldsControl.addCombo("properties", "property_comment_type", "comment type", commentTypeList, "description", "commentTypeId");
		txtComment = fieldsControl.addText("properties", "property_comment", "comment", 255, "");
		entityTypeList = EngineContext.getDataContext().getEntityProviderFactory().getEntityTypeProvider().getAll();
		cmbEntityType = fieldsControl.addCombo("properties", "property_type", "type", entityTypeList, "name", "entityTypeId");
		cmbEntityType.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(cmbEntityType.getSelectionIndex()>0) {
					EntityType entityType = entityTypeList.get(cmbEntityType.getSelectionIndex()-1);
					changeEntityType(entityType);
				} else
					setVisibleKeys(EngineContext.getDataContext().getEntityProviderFactory().getKeyDefinitionProvider().getAll(), false);
			}
		});
		keys = fieldsControl.addContainer("keys", "KEYS");
    	createButtons(parent);
    }
    private GridData getCellLayoutData() {
		GridData cellLayoutData = new GridData();
		cellLayoutData.horizontalAlignment = SWT.FILL;
		cellLayoutData.grabExcessHorizontalSpace = true;
		return cellLayoutData;
    }    
    private void createButtons(Composite parent) {
    	Composite buttons = new Composite(parent, SWT.NONE);
    	buttons.setLayout(new GridLayout(2, true));
    	buttons.setLayoutData(getCellLayoutData());    	
		cancelButton = new Button(buttons, SWT.NONE);
		cancelButton.setText("show all");
		cancelButton.setLayoutData(getCellLayoutData());
		acceptButton = new Button(buttons, SWT.NONE);
		acceptButton.setText("accept");
		acceptButton.setLayoutData(getCellLayoutData());
		cancelButton.addSelectionListener(new SelectionListener(){
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				cancel();
			}
		});
		acceptButton.addSelectionListener(new SelectionListener() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
			}
			@Override
			public void widgetSelected(SelectionEvent e) {
				accept();
			}
		});    	
    }    
    public abstract void accept();
    public abstract void cancel();
}
